#include <arpa/inet.h>
#include "RtpPacket.hpp"
namespace Hi {
bool RtpPacket::HasOneByteExtensions() const {
  return GetExtensionHeaderId() == 0xBEDE;
}

const RtpPacket::Header *RtpPacket::GetHeader() const
{
  return this->header;
}

const uint8_t *RtpPacket::GetData() const {
  return (const uint8_t *)this->header;
}

uint16_t RtpPacket::GetSequenceNumber() const {
  return uint16_t{ntohs(this->header->sequenceNumber)};
}

void RtpPacket::SetSequenceNumber(uint16_t seq) {
  this->header->sequenceNumber = uint16_t{htons(seq)};
}

uint32_t RtpPacket::GetTimestamp() const {
  return uint32_t{ntohl(this->header->timestamp)};
}

void RtpPacket::SetTimestamp(uint32_t timestamp) {
  this->header->timestamp = uint32_t{htonl(timestamp)};
}

bool RtpPacket::GetMark() const { 
	return (bool)(this->header->marker); 
}

uint32_t RtpPacket::GetSsrc() const {
  return uint32_t{ntohl(this->header->ssrc)};
}

void RtpPacket::SetSsrc(uint32_t ssrc) {
  this->header->ssrc = uint32_t{htonl(ssrc)};
}

uint8_t *RtpPacket::GetPayload() const { return this->payload; }

size_t RtpPacket::GetPayloadLength() const {
  return this->payloadLength;
}

bool RtpPacket::HasTwoBytesExtensions() const {
  return (GetExtensionHeaderId() & 0b1111111111110000) == 0b0001000000000000;
}

uint16_t RtpPacket::GetExtensionHeaderId() const {
	if (!this->extensionHeader)
		return 0;

	return uint16_t{ntohs(this->extensionHeader->id)};
}

size_t RtpPacket::GetExtensionHeaderLength() const {
  if (!this->extensionHeader) {
    return 0;
  }

  return static_cast<size_t>(ntohs(this->extensionHeader->length) * 4);
}

uint8_t RtpPacket::GetPayloadType() const {
  return this->header->payloadType;
}


size_t RtpPacket::GetSize() const {
	return this->size; 
}
bool RtpPacket::HasExtensionHeader() const {
  return (this->extensionHeader ? true : false);
}

 
  
void RtpPacket::SetPayloadDescriptorHandler(PayloadDescriptorHandler *handler) {
	this->payloadDescriptorHandler.reset(handler);
}
PayloadDescriptor* RtpPacket::payloadDescriptor() const {
	if (this->payloadDescriptorHandler == nullptr)	{
		return nullptr;
	}
	return this->payloadDescriptorHandler->payloadDescriptor();
}
} // namespace Hi End